﻿// <copyright file="TreeViewItemWithIcons.cs" company="Soumya Chattopadhyay">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <disclaimer> This software is intented for educational purposes only and not for 
// commerical or prodution use. The software is provided "as is", without warranty 
// of any kind, express or implied, including but not limited to the warranties of 
// merchantability, fitness for a particular purpose and noninfringement. In no event 
// shall the author(s) or copyright holder(s) be liable for any claim, damages or other 
// liability, whether in an action of contract, tort or otherwise, arising from, out of 
// or in connection with the software or the use or other dealings in the software.
// </disclaimer>

namespace My.CodePlex.TFSExplorer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    /// <summary>
    /// The treeviewitem with icons
    /// </summary>
    public class TreeViewItemWithIcons : TreeViewItem
    {
        #region Fields

        /// <summary>
        /// The icon soure
        /// </summary>
        private ImageSource iconSource;

        /// <summary>
        /// The item text
        /// </summary>
        private TextBlock textBlock;

        /// <summary>
        /// The item icon
        /// </summary>
        private Image icon;
        
        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the TreeViewItemWithIcons class
        /// </summary>
        public TreeViewItemWithIcons()
        {
            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;
            this.Header = stack;

            this.icon = new Image();
            this.icon.VerticalAlignment = VerticalAlignment.Center;
            this.icon.Visibility = Visibility.Visible;
            this.icon.Margin = new Thickness(0, 0, 4, 0);
            this.icon.Source = this.iconSource;
            stack.Children.Add(this.icon);

            // Add the HeaderText after the icon
            this.textBlock = new TextBlock();
            this.textBlock.VerticalAlignment = VerticalAlignment.Center;
            stack.Children.Add(this.textBlock);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the Selected Image for a TreeViewNode
        /// </summary>
        public ImageSource Icon
        {
            get
            {
                return this.iconSource;
            }

            set
            {
                this.iconSource = value;
                this.icon.Source = this.iconSource;
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Gets or sets the HeaderText of TreeViewWithIcons
        /// </summary>
        public string HeaderText
        {
            get
            {
                return this.textBlock.Text;
            }

            set
            {
                this.textBlock.Text = value;
            }
        }

        #endregion Event Handlers

        /// <summary>
        /// Event Handler on UnSelected Event
        /// </summary>
        /// <param name="args">The event arguments</param>
        protected override void OnUnselected(RoutedEventArgs args)
        {
            base.OnUnselected(args);
            this.icon.Source = this.iconSource;
        }

        /// <summary>
        /// Event Handler on Selected Event 
        /// </summary>
        /// <param name="args">The event arguments</param>
        protected override void OnSelected(RoutedEventArgs args)
        {
            base.OnSelected(args);
            this.icon.Source = this.iconSource;
        }
    }
}
